"""
    将python对象转换成json
"""

import json



""" python对象 转 json对象 """
def python_to_json():
    d = {
        'name': 'python书籍',
        'price': 62.3
    }
    rest = json.dumps(d, indent=2) # python对象 转 json对象 indent设置缩进
    print(rest)


""" json对象 转 python对象 """
def json_to_python():
    data = '''
        {
          "name": "Python书籍",
          "origin_price": 66,
          "pub_date": "2018-4-14 17:00:00",
          "store": ["京东", "淘宝"]
        }
    '''
    rest = json.loads(data) # json对象 转 python对象
    print(rest)
    print(rest['name'])


""" 将文件中的json对象 转 python对象 """
def json_to_python_from_file():
    file = open('./static/book.json', 'r', encoding='utf-8') # 读取文件
    con = file.read()
    rest = json.loads(con)
    print(rest)
    print(rest['name'])
    file.close() # 关闭读取文件，节约资源


""" main方法，用于执行python文件 """
if __name__ == '__main__':
    # python_to_json()
    # json_to_python()
    json_to_python_from_file()
